package com.skeleton.server.guide.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.skeleton.common.base.dto.ResultDto;
import com.skeleton.common.base.service.impl.BaseServiceImpl;
import com.skeleton.db.dao.IGuideCatgHotDao;
import com.skeleton.db.dao.IGuideCatgUserDao;
import com.skeleton.db.dao.IGuideItemHotDao;
import com.skeleton.db.dao.IGuideItemUserDao;
import com.skeleton.db.dto.GuideCatgHotDto;
import com.skeleton.db.dto.GuideCatgUserDto;
import com.skeleton.db.dto.GuideItemHotDto;
import com.skeleton.db.dto.GuideItemUserDto;
import com.skeleton.db.entity.GuideCatgHot;
import com.skeleton.db.entity.GuideCatgUser;
import com.skeleton.db.entity.GuideItemHot;
import com.skeleton.db.entity.GuideItemUser;
import com.skeleton.server.guide.service.IGuideService;

@Service
public class GuideServiceImpl extends BaseServiceImpl implements IGuideService {

	@Resource
	private IGuideCatgUserDao iGuideCatgUserDao;
	@Resource
	private IGuideItemUserDao iGuideItemUserDao;
	@Resource
	private IGuideCatgHotDao iGuideCatgHotDao;
	@Resource
	private IGuideItemHotDao iGuideItemHotDao;
	
	

	@Override
	public ResultDto doAddCatg(GuideCatgUserDto gcDto) {
		logger.info("-------GuideServiceImpl---doAddCatg-----------------");
		ResultDto rd = new ResultDto();

		GuideCatgUser gcEntity = gcDto.toEntity();
		iGuideCatgUserDao.insert(gcEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doLoadCatgList(String loginname) {
		logger.info("-------GuideServiceImpl---doLoadCatgList-----------------");
		ResultDto rd = new ResultDto();

		List<GuideCatgUser> gcList = iGuideCatgUserDao.selectCatgListByUser(loginname);
		for (int i = 0; i < gcList.size(); i++) {
			rd.addToDtoMap("GuideCatgUserDtoList", gcList.get(i).toDto());
		}

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doAddGuideItem(GuideItemUserDto giDto) {
		logger.info("-------GuideServiceImpl---doAddGuideItem-----------------");
		ResultDto rd = new ResultDto();

		GuideItemUser giEntity = giDto.toEntity();
		iGuideItemUserDao.updateAddSeqByCatg(giEntity.getCatg_user_id());
		iGuideItemUserDao.insert(giEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getGuideByUser(String loginname) {
		logger.info("-------GuideServiceImpl---getGuideByUser-----------------");
		ResultDto rd = new ResultDto();

		List<GuideCatgUser> gcList = iGuideCatgUserDao.selectCatgListByUser(loginname);

		for (int i = 0; i < gcList.size(); i++) {
			GuideCatgUser gc = gcList.get(i);
			List<GuideItemUser> giList = iGuideItemUserDao.selectByCatg(gc.getCatg_user_id());

			GuideCatgUserDto gcd = gc.toDto();
			List<GuideItemUserDto> gidList = new ArrayList<GuideItemUserDto>();
			for (int j = 0; j < giList.size(); j++) {
				gidList.add(giList.get(j).toDto());
			}
			gcd.setGidList(gidList);
			rd.addToDtoMap("GuideCatgUserDtoList", gcd);
		}

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doDeleteItemById(GuideItemUserDto giDto) {
		logger.info("-------GuideServiceImpl---doDeleteItemById-----------------");
		ResultDto rd = new ResultDto();

		GuideItemUser giEntity = giDto.toEntity();

		iGuideItemUserDao.delete(giEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doAddVisitCnt(GuideItemUserDto giDto) {
		logger.info("-------GuideServiceImpl---doAddVisitCnt-----------------");
		ResultDto rd = new ResultDto();

		GuideItemUser giEntity = giDto.toEntity();

		iGuideItemUserDao.updateVisitCnt(giEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getItemById(GuideItemUserDto giDto) {
		logger.info("-------GuideServiceImpl---getItemById-----------------");
		ResultDto rd = new ResultDto();

		GuideItemUser giEntity = giDto.toEntity();

		GuideItemUser gi = iGuideItemUserDao.selectOneById(giEntity);

		rd.addToDtoMap("GuideItemUserDto", gi.toDto());

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doModifyItemById(GuideItemUserDto giDto) {
		logger.info("-------GuideServiceImpl---doModifyItemById-----------------");
		ResultDto rd = new ResultDto();

		GuideItemUser giEntity = giDto.toEntity();

		iGuideItemUserDao.updateItem(giEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto getCatgById(GuideCatgUserDto gcDto) {
		logger.info("-------GuideServiceImpl---getCatgById-----------------");
		ResultDto rd = new ResultDto();

		GuideCatgUser gcEntity = gcDto.toEntity();

		GuideCatgUser gc = iGuideCatgUserDao.selectOneById(gcEntity);

		rd.addToDtoMap("GuideCatgUserDto", gc.toDto());
		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doModifyCatgById(GuideCatgUserDto gcDto) {
		logger.info("-------GuideServiceImpl---doModifyCatgById-----------------");
		ResultDto rd = new ResultDto();

		GuideCatgUser gcEntity = gcDto.toEntity();

		iGuideCatgUserDao.updateCatg(gcEntity);

		rd.setSuccess(true);
		return rd;
	}

	@Override
	public ResultDto doDeleteCatgById(GuideCatgUserDto gcDto) {
		logger.info("-------GuideServiceImpl---doDeleteCatgById-----------------");
		ResultDto rd = new ResultDto();

		GuideCatgUser gcEntity = gcDto.toEntity();

		int count = iGuideItemUserDao.countItemByCatgId(gcEntity.getCatg_user_id());

		if (count == 0) {
			iGuideCatgUserDao.delete(gcEntity);
			rd.setSuccess(true);
		} else {
			rd.setSuccess(false);
			rd.setErrorMSG("删除失败，请先删除该分类下的所有网址。");
		}

		return rd;
	}
	
	@Override
	public ResultDto getHotGuide() {
		logger.info("-------GuideServiceImpl---getHotGuide-----------------");
		ResultDto rd = new ResultDto();

		List<GuideCatgHot> gchList = iGuideCatgHotDao.selectHotGuideAll();

		for (int i = 0; i < gchList.size(); i++) {
			GuideCatgHot gch = gchList.get(i);
			GuideItemHot gih = new GuideItemHot();
			gih.setCatg_hot_id(gch.getCatg_hot_id());
			
			List<GuideItemHot> gihList = iGuideItemHotDao.selectItemByCatgId(gih);

			GuideCatgHotDto gchd = gch.toDto();
			List<GuideItemHotDto> gihdList = new ArrayList<GuideItemHotDto>();
			for (int j = 0; j < gihList.size(); j++) {
				gihdList.add(gihList.get(j).toDto());
			}
			gchd.setGihdList(gihdList);
			rd.addToDtoMap("GuideCatgHotDtoList", gchd);
		}

		rd.setSuccess(true);
		return rd;
	}

}
